<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>时间日期对象</title>
    <style>
      .jingdong {
        width: 190px;
        height: 260px;
        padding-top: 10px;
        background-image: url(jingdong.png);
        margin: 50px auto;
      }
      h2 {
        margin-top: 31px;
        font-size: 30px;
      }
      h2,
      p {
        color: #fff;
        font-weight: 700;
        text-align: center;
      }
      p {
        margin-top: 80px;
        font-size: 16px;
      }

      p strong {
        font-size: 18px;
      }

      .time {
        position: relative;
        width: 130px;
        height: 30px;
        margin: 0 auto;
      }

      span {
        position: relative;
        display: inline-block;
        width: 30px;
        height: 30px;
        line-height: 30px;
        text-align: center;
        background-color: #2f3430;
        color: #fff;
        margin: 0 5px;
        font-weight: bold;
        font-size: 18px;
      }

      span:nth-child(-n + 2)::after {
        content: ":";
        display: inline-block;
        position: absolute;
        bottom: 0;
        left: 27px;
        width: 20px;
        height: 30px;
        color: #fff;
        font-size: 18px;
      }
    </style>
  </head>

  <body>
    <div class="jingdong">
      <h2>京东秒杀</h2>
      <p><strong id="clock">21:00</strong>点场 距结束</p>
      <div class="time">
        <span id="shi"></span>
        <span id="fen"></span>
        <span id="miao"></span>
      </div>
    </div>
    <script>
      function countTime() {
        var now = new Date();
        var nowHour = now.getHours();
        var Session = "";
        var end = new Date();
        if (nowHour >= 6) {
          end.setMinutes(0);
          end.setSeconds(0);
          // 根据当前时间判断是哪场秒杀
          if (nowHour >= 6 && nowHour < 8) {
            Session = "8:00";
            // 确定结束时间
            end.setHours(8);
          }
          // 设定时间 需要补全代码
          if (nowHour >= 8 && nowHour < 10) {
            Session = "10:00";
            end.setHours(10);
          }
          if (nowHour >= 10 && nowHour < 12) {
            Session = "12:00";
            end.setHours(12);
          }
          if (nowHour >= 12 && nowHour < 14) {
            Session = "14:00";
            end.setHours(14);
          }
          if (nowHour >= 14 && nowHour < 16) {
            Session = "16:00";
            end.setHours(16);
          }
          if (nowHour >= 16 && nowHour < 18) {
            Session = "18:00";
            end.setHours(18);
          }
          if (nowHour >= 18 && nowHour < 20) {
            Session = "20:00";
            end.setHours(20);
          }
          if (nowHour >= 20 && nowHour < 22) {
            Session = "22:00";
            end.setHours(22);
          }
          if (nowHour >= 22) {
            Session = "00:00";
            end.setHours(24);
          }
        } else {
          Session = "暂时没有秒杀";
        }
        document.getElementById("clock").innerHTML = Session;

        var time = (end - now) /1000;                                     //计算时间差
        var hour = parseInt(time / 3600);                          //将时间差转换成小时数
        var minute = (Math.floor(time / 60)) % 60;                             //将时间差转换成分钟数
        var second = Math.floor(time % 60);                                          //将时间差转换成秒数
        // 若时分秒小于10，则在其前面补0显示
        hour = hour < 10 ? "0" + hour : hour;
        minute = minute < 10 ? "0" + minute : minute;
        second = second < 10 ? "0" + second : second;
        //获取页面元素并赋值
        document.getElementById("shi").innerHTML = hour;
        document.getElementById("fen").innerHTML = minute;
        document.getElementById("miao").innerHTML = second;
        // 递归调用自身函数触发倒计时
        setTimeout("countTime()", 1000);
      }
      // 页面加载时调用函数
      window.onload = countTime;
    </script>
  </body>
</html>
